package com.zecan.blog.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 全局controller 日志信息
 *
 * @ClassName BlogAspect
 * @Description
 * @date 2022/11/26 17:51
 * @Version 1.0
 */

@Aspect
@Slf4j
@Component
public class BlogControllerAspect {


    /**
     * 监控controller下路径调用信息
     *
     * @param joinPoint
     */
    @Before(value = "execution(* com.zecan.blog.controller.*.*(..))")
    public void controllerLogInfo(JoinPoint joinPoint) {
        // 获取请求信息
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        // 如果不为null 则继续往下执行
        assert attributes != null;

        HttpServletRequest request = attributes.getRequest();

        // 获取访问的ip
        String remoteHost = request.getRemoteHost();
        // 获取调用的方法
        String method = joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName();
        // 获取调用的时间
        log.info("IP:" + remoteHost + "-->调用了:" + method);
    }


}
